package com.x.component.widget;

import java.util.concurrent.CopyOnWriteArrayList;

import com.x.component.connectivity.FrameworkServicesModule;

import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.MediaMetadata;
import android.media.session.PlaybackState;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;

public class MediaWidgetHost {

	private static MediaWidgetHost mInstance = null;

	private static final Object mLock = new Object();

	private Context mContext;

	private static final String TAG = "MediaWidgetHost";

	private boolean hostInit;

	private HandlerThread mInstallThread;
	private Handler mInstallHandler;

	private Handler foregroundHandler;

	private AudioManager mAudioManager;

	private MediaWidgetHost() {
		foregroundHandler = new Handler(Looper.getMainLooper()) {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 101:
					MediaMetadata metadata = (MediaMetadata) msg.obj;
					Bitmap albumPicture = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
					String title = metadata.getString(MediaMetadata.METADATA_KEY_TITLE);
					String artist = metadata.getString(MediaMetadata.METADATA_KEY_ARTIST);
					String albumArtist = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST);
					String album = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM);
					String duration = metadata.getString(MediaMetadata.METADATA_KEY_DURATION);

					MediaWidgetInfo info = new MediaWidgetInfo(title, artist, album, albumPicture, 0);
					for (MediaWidgetTool tool : mMediaWidgetToolList) {
						tool.onMediaWidgetInfoChanged(info);
					}
					break;
				case 100:
					PlaybackState state = (PlaybackState) msg.obj;
					int status = state.getState() == PlaybackState.STATE_PLAYING ? 1 : 0;
					for (MediaWidgetTool tool : mMediaWidgetToolList) {
						tool.onMediaWidgetPlaybackStatusChanged(status);
					}
					break;

				default:
					break;
				}
			}
		};
		mInstallThread = new HandlerThread("MediaWidgetHostThread");
		mInstallThread.start();
		mInstallHandler = new Handler(mInstallThread.getLooper()) {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case 0:
					break;

				default:
					break;
				}
			}
		};
	}

	public static MediaWidgetHost getInstance() {
		if (null == mInstance) {
			synchronized (mLock) {
				if (null == mInstance) {
					mInstance = new MediaWidgetHost();
				}
			}
		}
		return mInstance;
	}

	public void init(Context context) {
		this.mContext = context;
		if (!isHostInit()&&this.mContext!=null) {
			setHostInit(true);
			mAudioManager = FrameworkServicesModule.provideAudioManager(context);
			ComponentName service = new ComponentName("com.x.carlauncher", "com.x.component.widget.MediaWidgetService");
			NotificationManager mNm = FrameworkServicesModule.provideNotificationManager(mContext);
			mNm.setNotificationListenerAccessGranted(service, true);
		}
	}

	public void sendMediaButtonEventPlay() {
		KeyEvent eventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
		KeyEvent eventUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
		mAudioManager.dispatchMediaKeyEvent(eventDown);
		mAudioManager.dispatchMediaKeyEvent(eventUp);
	}

	public void sendMediaButtonEventPause() {
		KeyEvent eventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
		KeyEvent eventUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
		mAudioManager.dispatchMediaKeyEvent(eventDown);
		mAudioManager.dispatchMediaKeyEvent(eventUp);
	}

	public void sendMediaButtonEventNext() {
		KeyEvent eventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
		KeyEvent eventUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT);
		mAudioManager.dispatchMediaKeyEvent(eventDown);
		mAudioManager.dispatchMediaKeyEvent(eventUp);
	}

	public void sendMediaButtonEventPrevious() {
		KeyEvent eventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
		KeyEvent eventUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
		mAudioManager.dispatchMediaKeyEvent(eventDown);
		mAudioManager.dispatchMediaKeyEvent(eventUp);
	}

	private CopyOnWriteArrayList<MediaWidgetTool> mMediaWidgetToolList = new CopyOnWriteArrayList<MediaWidgetTool>();

	public boolean setMediaWidgetTool(MediaWidgetTool MediaWidgetTool) {
		if (MediaWidgetTool != null && !mMediaWidgetToolList.contains(MediaWidgetTool)) {
			mMediaWidgetToolList.add(MediaWidgetTool);
			return true;
		}

		return false;
	}

	public boolean removeMediaWidgetTool(MediaWidgetTool MediaWidgetTool) {
		if (MediaWidgetTool != null && mMediaWidgetToolList.contains(MediaWidgetTool)) {
			mMediaWidgetToolList.remove(MediaWidgetTool);
			return true;
		}
		return false;
	}

	public boolean isHostInit() {
		return hostInit;
	}

	private void setHostInit(boolean hostInit) {
		this.hostInit = hostInit;
	}

	public Handler getForegroundHandler() {
		return foregroundHandler;
	}

}
